import 'package:ai_data_capture/bean/ConfigFileBean.dart';
import 'package:ai_data_capture/main.dart';
import 'package:ai_data_capture/util/toast_util.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'logic.dart';

class ConfigfilePage extends StatelessWidget {
  const ConfigfilePage({super.key});

  @override
  Widget build(BuildContext context) {
    final ConfigfileLogic logic = Get.find<ConfigfileLogic>();
    final state = logic.state;
    return Scaffold(
        appBar: AppBar(title: const Text("配置文件管理")),
        body: Padding(
            padding: const EdgeInsets.all(8.0),
            child: Column(
              children: [
                Container(
                    padding: const EdgeInsets.symmetric(horizontal: 20),
                    decoration: BoxDecoration(
                        color: Colors.cyan.shade100,
                        borderRadius: BorderRadius.circular(10)),
                    child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          const SizedBox(
                            height: 10,
                            width: double.infinity,
                          ),
                          Obx(() {
                            return Text(
                                "配置id:${state.currentConfig.value?.config_id}",
                                style: const TextStyle(
                                    fontWeight: FontWeight.bold, fontSize: 20));
                          }),
                          Padding(
                              padding: const EdgeInsets.symmetric(vertical: 10),
                              child: Obx(() {
                                return Text(
                                    "配置描述：${state.currentConfig.value?.description ?? ""}");
                              })),
                          const SizedBox(height: 10)
                        ])),
                const SizedBox(height: 10),
                Expanded(
                  child: Obx(() {
                    if (state.list.isEmpty) {
                      return const Center(child: Text("当前没有配置文件"));
                    }
                    return ListView.builder(
                        itemCount: state.list.length,
                        itemBuilder: (BuildContext context, int index) {
                          var bean = state.list[index];
                          return InkWell(
                              onTap: () {},
                              child: Padding(
                                  padding: const EdgeInsets.all(8.0),
                                  child: Row(children: [
                                    Expanded(
                                        child: Column(
                                            crossAxisAlignment:
                                                CrossAxisAlignment.start,
                                            children: [
                                          Text("id:${bean.id}",
                                              style: const TextStyle(
                                                  fontWeight: FontWeight.bold,
                                                  fontSize: 20)),
                                          Text("配置描述：${bean.description}")
                                        ])),
                                    InkWell(
                                        onTap: () {
                                          logic.setupConfig(bean);
                                        },
                                        child: Padding(
                                            padding: const EdgeInsets.all(8.0),
                                            child: Obx(() {
                                              return state.currentConfig.value
                                                          ?.config_id ==
                                                      bean.id
                                                  ? const Icon(
                                                      Icons.check_circle,
                                                      color: Colors.blue)
                                                  : const Icon(Icons
                                                      .check_circle_outline);
                                            }))),
                                    InkWell(
                                        onTap: () {
                                          Get.toNamed(RoutePath.editConfig,
                                              arguments: {
                                                "bean": bean,
                                                "isEdit": true,
                                              })?.then((r) {
                                            if (r is ConfigFileBean) {
                                              r.id = bean.id;
                                              logic.addConfigFile(r,
                                                  isEdit: true);
                                            }
                                          });
                                        },
                                        child: const Padding(
                                            padding: EdgeInsets.all(8.0),
                                            child: Icon(
                                                Icons.edit_location_alt_rounded,
                                                color: Colors.blue))),
                                    InkWell(
                                        onTap: () {
                                          if (state.currentConfig.value
                                                  ?.config_id ==
                                              bean.id) {
                                            showToast(
                                                "当前正在使用配置:${state.currentConfig.value?.description}");
                                            return;
                                          }
                                          logic.removeFile(bean.id);
                                        },
                                        child: const Padding(
                                            padding: EdgeInsets.all(8.0),
                                            child: Icon(Icons.remove_circle,
                                                color: Colors.red)))
                                  ])));
                        });
                  }),
                ),
              ],
            )),
        floatingActionButton: FloatingActionButton(
            shape: const CircleBorder(),
            onPressed: () {
              Get.toNamed(RoutePath.editConfig, arguments: {
                "bean": null,
                "isEdit": false,
              })?.then((r) {
                if (r is ConfigFileBean) {
                  logic.addConfigFile(r, isEdit: false);
                }
              });
            },
            child: const Icon(Icons.add)));
  }
}
